package cn.hxzy.company.service.impl;

import cn.hxzy.company.dao.MemberDao;
import cn.hxzy.company.entity.Member;
import cn.hxzy.company.entity.Work;
import cn.hxzy.company.entity.vo.PageBean;
import cn.hxzy.company.service.MemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Member)表服务实现类
 *
 * @author makejava
 * @since 2020-08-03 22:29:56
 */
@Service("memberService")
public class MemberServiceImpl implements MemberService {
    @Resource
    private MemberDao memberDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Member queryById(Integer id) {
        return this.memberDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Member> queryAllByLimit(int offset, int limit) {
        return this.memberDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param member 实例对象
     * @return 实例对象
     */
    @Override
    public Member insert(Member member) {
        this.memberDao.insert(member);
        return member;
    }

    /**
     * 修改数据
     *
     * @param member 实例对象
     * @return 实例对象
     */
    @Override
    public Member update(Member member) {
        this.memberDao.update(member);
        return this.queryById(member.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.memberDao.deleteById(id) > 0;
    }

    /**
     * 查询全部
     *
     * @return
     */
    @Override
    public List<Member> queryAll() {
        return memberDao.findAll();
    }

    /**
     * 分页处理
     *
     * @return
     */
    @Override
    public void queryPage(PageBean pageBean) {
        Integer size = pageBean.getSize();
        Integer page = pageBean.getPage();
        List<Member> members = memberDao.queryAllByLimit((page - 1) * size, size);
        pageBean.setList(members);
        long count = memberDao.countAll();
        pageBean.setCount(count);
        pageBean.setPageCount((int) (count%size==0?(count/size):(count/size+1)));
    }

    /**
     * 模糊查询->id
     * @param pageBean
     * @param id
     */
    @Override
    public void queryPageWithId(PageBean pageBean, String id) {
        Integer page = pageBean.getPage();
        Integer size = pageBean.getSize();
        List<Member> works = memberDao.queryLimitWithId("%" + id + "%", (page - 1) * size, size);
        pageBean.setList(works);
        long count = memberDao.countId("%" + id + "%");
        pageBean.setCount(count);
        pageBean.setPageCount((int) (count % size == 0 ? (count / size) : (count / size + 1)));
    }

    @Override
    public void queryPageWithName(PageBean pageBean, String name) {
        Integer page = pageBean.getPage();
        Integer size = pageBean.getSize();
        List<Member> works = memberDao.queryLimitWithName("%" + name + "%", (page - 1) * size, size);
        pageBean.setList(works);
        long count = memberDao.countName("%" + name + "%");
        pageBean.setCount(count);
        pageBean.setPageCount((int) (count % size == 0 ? (count / size) : (count / size + 1)));
    }

    @Override
    public void queryPageWithMemberWork(PageBean pageBean, String memberWork) {
        Integer page = pageBean.getPage();
        Integer size = pageBean.getSize();
        List<Member> works = memberDao.queryLimitWithMemberWork("%" + memberWork + "%", (page - 1) * size, size);
        System.out.println(works);
        pageBean.setList(works);
        long count = memberDao.countMemberWork("%" + memberWork + "%");
        pageBean.setCount(count);
        pageBean.setPageCount((int) (count % size == 0 ? (count / size) : (count / size + 1)));
    }
}